/////////////////////////////////////////////////////////////////////////////
// Original code from libhdfs3. Copyright (c) 2013 - 2014, Pivotal Inc.
// All rights reserved. Author: Zhanwei Wang
/////////////////////////////////////////////////////////////////////////////
//  Modifications by Kumo Inc.
// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//



#ifndef _HDFS_LIBHDFS3_CLIENT_OUTPUTSTREAM_H_
#define _HDFS_LIBHDFS3_CLIENT_OUTPUTSTREAM_H_

#include <kmhdfs/client/file_system.h>
#include <kmhdfs/client/ec_policy.h>

namespace Hdfs {

namespace Internal {
class OutputStreamInter;
}

/**
 * A output stream used to write data to hdfs.
 */
class OutputStream {
public:
    /**
     * Construct a new OutputStream.
     */
    OutputStream();
    OutputStream(std::shared_ptr<ECPolicy> ecPolicy);

    /**
     * Destroy a OutputStream instance.
     */
    ~OutputStream();

    /**
     * To create or append a file.
     * @param fs hdfs file system.
     * @param path the file path.
     * @param pair the result of create or append.
     * @param flag creation flag, can be Create, Append or Create|Overwrite.
     * @param permission create a new file with given permission.
     * @param createParent if the parent does not exist, create it.
     * @param replication create a file with given number of replication.
     * @param blockSize  create a file with given block size.
     * @param fileId  the file id.
     */
    void open(FileSystem & fs, const char * path,
              std::pair<std::shared_ptr<LocatedBlock>, std::shared_ptr<Hdfs::FileStatus>> & pair,
              int flag = Create, const Permission permission = Permission(0644),
              bool createParent = false, int replication = 0, int64_t blockSize = 0,
              int64_t fileId = 0);

    void open(FileSystem & fs, const char * path,
              int flag = Create, const Permission permission = Permission(0644),
              bool createParent = false, int replication = 0, int64_t blockSize = 0);

    /**
     * To append data to file.
     * @param buf the data used to append.
     * @param size the data size.
     */
    void append(const char * buf, int64_t size);

    /**
     * Flush all data in buffer and waiting for ack.
     * Will block until get all acks.
     */
    void flush();

    /**
     * return the current file length.
     * @return current file length.
     */
    int64_t tell();

    /**
     * the same as flush right now.
     */
    void sync();

    /**
     * close the stream.
     */
    void close();

private:
    Internal::OutputStreamInter * impl;

};

}

#endif /* _HDFS_LIBHDFS3_CLIENT_OUTPUTSTREAM_H_ */
